// src/store/modules/user.js
const state = {
  userInfo: JSON.parse(localStorage.getItem("user")) || null,
};

const mutations = {
  SET_USER(state, user) {
    state.userInfo = user;
    localStorage.setItem("user", JSON.stringify(user));
  },
  LOGOUT(state) {
    state.userInfo = null;
    localStorage.removeItem("user");
  },
};

const actions = {
  setUser({ commit }, user) {
    commit("SET_USER", user);
  },
  logout({ commit }) {
    commit("LOGOUT");
  },
};

const getters = {
  isLogin: (state) => !!state.userInfo,
  user: (state) => state.userInfo,
};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters,
};
